﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		BaseViewData.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Web;
using Dexter.Domain.Model;
using Dexter.Services;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.ViewsData {
	public class BaseViewData : DynamicObject {
		readonly IDictionary <String , object> customProperties = new Dictionary <String , object> ( );
		readonly object lockObject = new object ( );
		readonly IUrlBuilder urlBuilder;
		readonly IWebUtils webUtils;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "BaseViewData" /> class.
		/// </summary>
		/// <param name = "webUtils">The web utils.</param>
		/// <param name = "configuration">The configuration.</param>
		/// <param name = "urlBuilder">The URL builder.</param>
		public BaseViewData ( IWebUtils webUtils , SiteConfiguration configuration , IUrlBuilder urlBuilder ) {
			Configuration = configuration;
			this.webUtils = webUtils;
			this.urlBuilder = urlBuilder;
		}

		/// <summary>
		/// 	Gets or sets the configuration.
		/// </summary>
		/// <value>The configuration.</value>
		public SiteConfiguration Configuration { get; private set; }

		/// <summary>
		/// 	Return the blog name from the <see cref = "SiteConfiguration" /> <c>class</c>.
		/// </summary>
		/// <value>The blog title.</value>
		public string BlogTitle {
			get { return this.Configuration.BlogName; }
		}

		/// <summary>
		/// 	Gets the web utils.
		/// </summary>
		/// <value>The web utils.</value>
		public IWebUtils WebUtils {
			get { return webUtils; }
		}

		/// <summary>
		/// 	Gets the URL builder.
		/// </summary>
		/// <value>The URL builder.</value>
		public IUrlBuilder UrlBuilder {
			get { return urlBuilder; }
		}

		/// <summary>
		/// 	Gets the context.
		/// </summary>
		/// <value>The context.</value>
		public HttpContextBase Context {
			get { return this.WebUtils.Contex; }
		}

		/// <summary>
		/// 	Provides the implementation for operations that get member values. Classes derived from the <see cref = "T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
		/// </summary>
		/// <param name = "binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref = "T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
		/// <param name = "result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name = "result" />.</param>
		/// <returns>
		/// 	true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
		/// </returns>
		public override bool TryGetMember ( GetMemberBinder binder , out object result ) {
			if ( customProperties.ContainsKey ( binder.Name ) ) {
				result = customProperties[ binder.Name ];
			}
			else {
				result = binder.ReturnType.IsValueType
				         	? Activator.CreateInstance ( binder.ReturnType )
				         	: null;
				customProperties.Add ( binder.Name , result );
			}
			return true;
		}

		/// <summary>
		/// 	Provides the implementation for operations that set member values. Classes derived from the <see cref = "T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as setting a value for a property.
		/// </summary>
		/// <param name = "binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member to which the value is being assigned. For example, for the statement sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the <see cref = "T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
		/// <param name = "value">The value to set to the member. For example, for sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the <see cref = "T:System.Dynamic.DynamicObject" /> class, the <paramref name = "value" /> is "Test".</param>
		/// <returns>
		/// 	true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
		/// </returns>
		public override bool TrySetMember ( SetMemberBinder binder , object value ) {
			lock ( lockObject ) {
				customProperties[ binder.Name ] = value;
			}

			return true;
		}
	}
}
